/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurchaseService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 采购收货
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/pur_col")
@RestController
public class XqcPurColController {

    /** 采购服务 */
    @Autowired
    private IXqcPurchaseService purchaseService;

    /**
     * 保存采购收货单
     */
    @PostMapping("/upload_purcol_bill")
    @VtuzxApi
    public Object uploadPurColBill(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        purchaseService.uploadPurColBill(_userId, name, vFileList.get(0).file());
        return null;
    }

    /**
     * 保存采购收货单
     */
    @PostMapping("/save_purcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "materialNo", label = "物料", notNull = true),
            @VtuzxValidator(key = "batchNo", label = "批号", notNull = true),
            @VtuzxValidator(key = "produceDate", label = "生产日期", notNull = true),
            @VtuzxValidator(key = "purCount", label = "总重", notNull = true),
    } )
    public Object savePurColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return purchaseService.savePurColBill(_userId, name, param);
    }

    /**
     * 保存采购收货单
     */
    @PostMapping("/delete_purcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object deletePurColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        purchaseService.deletePurColBill(_userId, billNo);
        return null;
    }

    /**
     * 审核采购收货单
     */
    @PostMapping("/audit_purcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object auditPurColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        String name = VtuzxUtil.getString(token, "name");
        return purchaseService.auditPurColBill(_userId, name, billNo);
    }

    /**
     * 反审核采购收货单
     */
    @PostMapping("/unaudit_purcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object unauditPurColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purchaseService.unauditPurColBill(_userId, billNo);
    }

    /**
     * 根据RFID从采购收货单中获取物料
     */
    @PostMapping("/get_material_by_rfid")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "rfid", label = "RFID", notNull = true)
    } )
    public Object getMaterialByRfid(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid");
        return purchaseService.getPurColMaterialByRfid(rfidList);
    }

    /**
     * 根据箱号从采购收货单中获取物料
     */
    @PostMapping("/get_material_by_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true)
    } )
    public Object getMaterialByBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        return purchaseService.getPurColMaterial(boxNo);
    }

    /**
     * 获取采购收货单
     */
    @PostMapping("/get_purcol_bill")
    @VtuzxApi
    public Object getPurColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return purchaseService.getPurColBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取采购收货单详细
     */
    @PostMapping("/get_purcol_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getPurColBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purchaseService.getPurColBillDetail(_userId, billNo);
    }

    /**
     * 下载采购收货单列表(用于打印二维码)
     */
    @PostMapping("/download_purcol_bill_barcode")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object downloadPurColBillBarcode(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return purchaseService.downloadPurColBillBarcode(_userId, billNo);
    }

    /**
     * 下载采购收货单列表(多单据, 用于打印二维码)
     */
    @PostMapping("/download_purcol_bill_barcode_list")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNoList", label = "单号列表", notNull = true)
    } )
    public Object downloadPurColBillBarcodeList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        return purchaseService.downloadPurColBillBarcodeList(_userId, billNoList);
    }

}
